#include "tabledriver.h"
#include "aprcfg.h"

const ushort kMasterItems[]	= { PM_POLH, PM_HUE, PM_HUEI, PM_VLV, 
								PM_VLI, PM_DLMX, PM_DLMY, 0 };
const ushort kHueItems[] = { PM_POLH, PM_HUE, PM_HUEI, 0 };
const ushort kVlItems[]  = { PM_VLV, PM_VLI, 0 };
const ushort kDlmItems[] = { PM_DLMX, PM_DLMY, 0 };


//namespace {
//
//const prm_tbl_s *Param(ushort prm_code)
//{
//	const prm_tbl_s *tbl_iter = paramTblm;
//	while (tbl_iter->commandName != NULL) {
//		if (tbl_iter->code == prm_code) {
//			return tbl_iter;
//		}
//		++tbl_iter;
//	}
//	return NULL;
//}
//
//} // namespace

namespace TableDriver {

const prm_tbl_s *Param(ushort prm_code)
{
	const prm_tbl_s *tbl_iter = paramTblm;
	while (tbl_iter->commandName != NULL) {
		if (tbl_iter->code == prm_code) {
			return tbl_iter;
		}
		++tbl_iter;
	}
	return NULL;
}

ushort ParamCode( const std::string& prm_name, para_attr_e prm_attr )
{
	const prm_tbl_s *tbl_iter = paramTblm;
	while (tbl_iter->commandName != NULL) {
		if (tbl_iter->commandName == prm_name && tbl_iter->attr == prm_attr) {
			return tbl_iter->code;	
		}
		++tbl_iter;
	}
	return INVALID;
}



std::string ParamName(ushort prm_code)
{
//	std::string result;
//	const prm_tbl_s *tbl_iter = paramTblm;
//	while (tbl_iter->commandName != NULL) {
//		if (tbl_iter->code == prm_code) {
//			result = tbl_iter->commandName;
//			break;
//		}
//		++tbl_iter;
//	}
//	return result;
	std::string name;
	const prm_tbl_s *prm = Param(prm_code);
	if (prm != NULL) {
		name = prm->commandName;
	}
	return name;
}



std::vector<std::string> ParamList(para_attr_e attr)
{
	std::vector<std::string> result;
	std::string prm_name;
	const prm_tbl_s *tbl_iter = paramTblm;
	while ( tbl_iter->commandName != NULL ) {
		if ( tbl_iter->attr == attr ) {
			prm_name = tbl_iter->commandName;
			result.push_back(prm_name);
		}
		++tbl_iter;
	}
	return result;
}



std::vector<std::string> RangeList(para_attr_e attr, char bmode, char mmode)
{
	std::vector<std::string> result;
	std::string rng_name;
	const prm_tbl_s *tbl_iter = paramTblm;
	while ( tbl_iter->commandName != NULL ) {
		bool bmode_match = ( '0' == bmode || tbl_iter->bmode == bmode );
		bool mmode_match = ( '0' == mmode || tbl_iter->mmode == mmode );
		if ( tbl_iter->attr == attr && bmode_match && mmode_match ) {
			rng_name = tbl_iter->commandName;
			result.push_back( rng_name );
		}
		++tbl_iter;
	}
	return result;
}


std::vector<std::string> ItemRangeList(int item_code, para_attr_e attr)
{
	const prm_tbl_s *prm = Param(item_code);
	char mode = 0;
	if (prm != NULL) {
		if (PM_MRANGE_ATTR == attr) {
			mode = prm->mmode;
		} else if (PM_BRANGE_ATTR == attr) {
			mode = prm->bmode;
		}
	}
	return RangeList(attr, mode, '0');
}

const rng_tbl_s *Range( ushort code )
{
	const rng_tbl_s *rng_iter = rangeTblm;
	while ( rng_iter->dfmt != NULL ) {
		if ( rng_iter->code == code ) {
			return rng_iter;
		}
		++rng_iter;
	}
	return NULL;
}



//std::string RangeValueLower( const rng_tbl_s *range )
//{
//	return FormatRangeValue( range->lowlimit, range );
//}
//
//
//
//std::string RangeValueUpper( const rng_tbl_s *range )
//{
//	return FormatRangeValue( range->hilimit, range );
//}
//
//
//
//std::string FormatRangeValue( double value, const rng_tbl_s *range )
//{
//	char val_str[MAX_VAL_LEN];
//	sprintf( val_str, range->dfmt, value );	
//	return val_str;
//}


int DeviceIndex( ushort code )
{
	const devtbl_s *dev_iter = devTblm;
	int dev_index = 0;
	while ( dev_iter->code != -1 ) {
		if ( dev_iter->code == code ) {
			return dev_index;
		}
		++dev_iter;
		++dev_index;
	}
	return INVALID;
}

const devtbl_s *DeviceTable()
{
	return devTblm;
}

bool IsMasterItem( ushort item_code )
{
	const ushort *master_code = kMasterItems;
	while ( *master_code != 0 ) {
		if ( *master_code == item_code ) {
			return true;
		}
		++master_code;
	}
	return false;
}


bool IsHueItem( ushort item_code )
{
	const ushort *hue_code = kHueItems;
	while ( *hue_code != 0 ) {
		if ( *hue_code == item_code ) {
			return true;
		}
		++hue_code;
	}
	return false;
}


bool IsVlItem( ushort item_code )
{
	const ushort *vl_code = kVlItems;
	while ( *vl_code != 0 ) {
		if ( *vl_code == item_code ) {
			return true;
		}
		++vl_code;
	}
	return false;
}



bool IsDlmItem( ushort item_code )
{
	const ushort *dlm_code = kDlmItems;
	while ( *dlm_code != 0 ) {
		if ( *dlm_code == item_code ) {
			return true;
		}
		++dlm_code;
	}
	return false;
}



} // namespace TableDriver
